package cn.tx.controller;

import cn.tx.entity.Result;
import cn.tx.service.SetmealService;
import cn.tx.utils.FastDFSClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Snake Eyes
 * @PROJECT_NAME: health_parent
 * @DESCRIPTION:
 * @USER: 11647
 * @DATE: 2021/8/29 17:57
 * @Email: 1164763541@qq.com
 */
@RestController
public class UploadController {
//    @Value("${FILE_SERVER_URL}")
    private String path = "http://192.168.25.133/";
    //文件名称
    private String filename;
    /*@Autowired
    private SetmealService setmealService;
*/
    /**
     * @return
     * @author :11647
     * @date ：Created in 2021/6/29  18:21
     * @description： 上传方法
     * @modified By：11647
     * @version: 0.1
     */
    @RequestMapping("upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {

        FastDFSClient client = null;
        try {
            client = new FastDFSClient("classpath:config/fdfs_client.conf");
            //获取到上传文件的名称
            filename = imgFile.getOriginalFilename();
            //获取后缀名
            String extName = filename.substring(filename.lastIndexOf(".") + 1);
            String s = client.uploadFile(imgFile.getBytes(), extName);
            String url = path + s;
            System.out.println("url：" + url);
            //拼接文件完整路径
            //存储所有文件路径

            //创建脏文件数据路径


            return new Result(true, "上传成功", url);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, "上传失败");
        }
    }


}

